package com.xydtech.ecard.ecard.controller;

import com.xydtech.common.annotation.PassToken;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardServeModuleConfig;
import com.xydtech.ecard.config.domain.EcardServeModuleNews;
import com.xydtech.ecard.config.service.IEcardServeModuleConfigService;
import com.xydtech.ecard.config.service.IEcardServeModuleNewsService;
import com.xydtech.ecard.ecard.domain.EcardApiVisitRecord;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysWechatAppletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 电子名片-服务模块Controller
 *
 * @author yjs
 * @date 2022/6/24 13:23
 */

@Controller
@Api(tags = "电子名片-电子名片-服务信息")
@RequestMapping("/ecardSaasApi/ecard/serveModule")
public class EcardServeModuleController extends EcardApiBaseController {
    @Autowired
    private IEcardServeModuleConfigService ieCardServeModuleConfigService;
    @Autowired
    private ISysWechatAppletService wechatAppletService;
    @Autowired
    private IEcardServeModuleNewsService ecardServeModuleNewsService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * 电子名片-电子名片-获取服务数据
     *
     * @return
     */
    @PassToken
    @ApiOperation(value = "电子名片-电子名片-获取服务数据", notes = "电子名片-电子名片-获取服务数据")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiVisitRecord.class)})
    @GetMapping("/findServeModuleList")
    @ResponseBody
    public ResponseModel findServeModuleList() {
        String salt = getSalt();//盐值
        String currentUserId = getCurrentUserId();
        EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
        //当前小程序id
        SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
        String appId = wechatApplet.getAppid();
        if (StringUtils.isEmpty(appId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }
        //当前小程序对应的法人机构ID
        Long parentDeptId = wechatApplet.getDeptId();

        List<EcardServeModuleConfig> ecardServeModuleList = ieCardServeModuleConfigService.findServeModuleList(parentDeptId);
        return ResponseModel.ok(ecardServeModuleList);
    }

    /**
     * 电子名片-电子名片-获取服务模块内部新闻列表
     *
     * @return
     */
    @ApiOperation(value = "电子名片-电子名片-获取服务模块内部新闻列表", notes = "电子名片-电子名片-获取服务模块内部新闻列表")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiVisitRecord.class)})
    @GetMapping("/findServeModuleNewsList")
    @ResponseBody
    public ResponseModel findServeModuleNewsList(@RequestParam(required = false) String functionId) {
        if (StringUtils.isEmpty(functionId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }
        EcardServeModuleNews ecardServeModuleNews = new EcardServeModuleNews();
        ecardServeModuleNews.setFunctionId(Long.valueOf(functionId));
        startPage();
        List<EcardServeModuleNews> list = ecardServeModuleNewsService.selectEcardServeModuleNewsList(ecardServeModuleNews);
        return getTableResponse(list);
    }

    /**
     * 电子名片-电子名片-获取服务模块内部新闻详情
     *
     * @return
     */
    @PassToken
    @ApiOperation(value = "电子名片-电子名片-获取服务模块内部新闻详情", notes = "电子名片-电子名片-获取服务模块内部新闻详情")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiVisitRecord.class)})
    @GetMapping("/findServeModuleNewsInfo")
    @ResponseBody
    public ResponseModel findServeModuleNewsInfo(@RequestParam(required = false) String id) {
        if (StringUtils.isEmpty(id)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }
        EcardServeModuleNews ecardServeModuleNews = ecardServeModuleNewsService.selectEcardServeModuleNewsById(Long.valueOf(id));
        return ResponseModel.ok(ecardServeModuleNews);
    }
}
